/**
 * Chesney's Fireplace System Includes New Supplier, New Fireplace Item, Supplier and Fireplace Update, 
 * Supplier and Fireplace Reports, Login, main menu and Tutorial.
 * 
 * It also Includes Database connection, Database Extension and the two classes which are MySupplier and My Fireplace.
 *  
 * Copyright (C) Bournemouth University 
 * 
 * @author Ruth Shepperd, Ari Lopez
 * 
 * @version 1.0
 */
package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;



import Database.DatabaseExtended;
import Database.MySupplier;


public class NewSupplierScreen extends JFrame {
	//Attributes
	private String id;
	private JPanel contentPane;
	private JTextField telephoneTextField;
	private JTextField postcodeTextField;
	private JTextField emailTextField;
	private JTextField townTextField;
	private JTextField sNameTextField;
	private JTextArea addressTextArea;
	private JButton btnSubmitAndSave;
	private JButton btnClear;
	private JButton btnMainMenu;
	MySupplier supplier;
	private JFrame thisFrame = this;

	/**
	 * Constructor method to set all of the supplier details
	 * @param supplier (My Supplier)
	 */
	public NewSupplierScreen(MySupplier supplier) {
		this();
		this.supplier=supplier;
		
		id = supplier.getSupplierID();
		sNameTextField.setText(supplier.getSupplierName());
		addressTextArea.setText(supplier.getAddress());
		townTextField.setText(supplier.getTown());
		postcodeTextField.setText(supplier.getPostcode());
		telephoneTextField.setText(supplier.getTelephoneNo());
		emailTextField.setText(supplier.getEmail());
	}

	/**
	 * Create the frame.
	 */
	public NewSupplierScreen() {
		setVisible(true);
		setTitle("New Supplier");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 366, 477);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setResizable(false);
		
		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Enter Supplier Details", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		//Save and Submit Button
		btnSubmitAndSave = new JButton("Submit and Save");
		btnSubmitAndSave.addActionListener(new ActionListener() {
			/**
			 * Get the data of all details
			 */
			//
			public void actionPerformed(ActionEvent arg0) {
				
				//Set up new array that will contain the input data of the screen
				String[] data = new String[7];	
				//if id is not null set up to null
				try {
					data[0] = supplier.getSupplierID();
				} catch (Exception e) {
					data[0] = null;
				}
				//get the data from the screen fields and put in the array.
				data[1] = sNameTextField.getText();
				data[2] = addressTextArea.getText();
				data[3] = townTextField.getText();
				data[4] = postcodeTextField.getText();
				data[5] = telephoneTextField.getText();
				data[6] = emailTextField.getText();
				
				//If Statement when supplier name text-field is empty
				if(data[1].equals("")){
					JOptionPane.showMessageDialog(null, "The supplier name field cannot be empty");
					
				}else{
				//Save the Supplier
				DatabaseExtended.saveSupplier(new MySupplier(data));
				thisFrame.dispose();
				}
				
				}
		
			});
		
		//Clear Button
		btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			/**
			 * Clear the text-fields
			 */
			public void actionPerformed(ActionEvent e) {
				sNameTextField.setText("");
				addressTextArea.setText("");
				townTextField.setText("");
				postcodeTextField.setText("");
				telephoneTextField.setText("");
				emailTextField.setText("");
			}
		});
		
		//Main Menu Button
		btnMainMenu = new JButton("Main Menu");
		btnMainMenu.addActionListener(new ActionListener() {
			/**
			 * Dispose the screen
			 */
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(btnSubmitAndSave)
									.addGap(18)
									.addComponent(btnClear)
									.addPreferredGap(ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
									.addComponent(btnMainMenu))
								.addComponent(panel, GroupLayout.PREFERRED_SIZE, 329, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(37)
							.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 272, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnSubmitAndSave, GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
						.addComponent(btnMainMenu, GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
						.addComponent(btnClear, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))
					.addGap(21))
		);
		
		JLabel lblName = new JLabel("Name:");
		
		JLabel lblAddress = new JLabel("Address:");
		
		JLabel lblTown = new JLabel("Town:");
		
		JLabel lblPostcode = new JLabel("Postcode:");
		
		JLabel lblTelephone = new JLabel("Telephone:");
		
		JLabel lblEmail = new JLabel("Email:");
		
		telephoneTextField = new JTextField();
		telephoneTextField.setColumns(10);
		
		postcodeTextField = new JTextField();
		postcodeTextField.setColumns(10);
		
		emailTextField = new JTextField();
		emailTextField.setColumns(10);
		
		townTextField = new JTextField();
		townTextField.setColumns(10);
		
		addressTextArea = new JTextArea();
		addressTextArea.setTabSize(0);
		addressTextArea.setWrapStyleWord(true);
		addressTextArea.setLineWrap(true);
		
		sNameTextField = new JTextField();
		sNameTextField.setColumns(10);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addComponent(lblTelephone)
						.addComponent(lblPostcode)
						.addComponent(lblEmail)
						.addComponent(lblTown)
						.addComponent(lblAddress)
						.addComponent(lblName))
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addComponent(addressTextArea, GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
						.addComponent(emailTextField, GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
						.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING, false)
							.addComponent(townTextField, Alignment.LEADING)
							.addComponent(postcodeTextField, Alignment.LEADING)
							.addComponent(telephoneTextField, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE))
						.addComponent(sNameTextField, GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblName)
						.addComponent(sNameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addComponent(lblAddress)
						.addComponent(addressTextArea, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblTown)
						.addComponent(townTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPostcode)
						.addComponent(postcodeTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblTelephone)
						.addComponent(telephoneTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblEmail)
						.addComponent(emailTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel_1.setLayout(gl_panel_1);
		
		JLabel lblNewSupplier = new JLabel("New Supplier");
		lblNewSupplier.setFont(new Font("Arial", Font.BOLD, 18));
		lblNewSupplier.setHorizontalAlignment(SwingConstants.CENTER);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewSupplier, GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewSupplier, GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
					.addContainerGap())
		);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}
}
